<?php

class PublicController extends Controller {

    public function actionIndex() {

        $this->render('index', array(

        ));
    }

    public function actionHome(){
        $this->render('index', array(

        ));
    }

    /**
     * Displays the login page
     */
    public function actionLogin() {
	
        $this->layout = '/layouts/login';
        $model = new LoginForm;

        // if it is ajax validation request
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }

        // collect user input data
        if (isset($_POST['LoginForm'])) {
            $model->attributes = $_POST['LoginForm'];
            // validate user input and redirect to the previous page if valid
            if ($model->validate() && $model->login()){
				if(Yii::app()->user->checkAccess(Constant::ROLE_ADMIN)){
					$this->redirect(array('task/index'));
				}else{
					$this->redirect(array('public/home'));
				}
				
			}
                
        }
        // display the login form
        $this->render('login', array(
            'model' => $model
        ));
    }

    /**
     * Logs out the current user and redirect to homepage.
     */
    public function actionLogout()
    {
        Yii::app()->user->logout();
        $this->redirect(Yii::app()->homeUrl);
    }

    /**
     * This is the action to handle external exceptions.
     */
    public function actionError()
    {
//        $this->layout = 'public';
        if($error=Yii::app()->errorHandler->error)
        {
            if(Yii::app()->request->isAjaxRequest)
                echo $error['message'];
            else
                $this->render('error', $error);
        }
    }

}